<?php
/**
 * This file contains all of the default achievements.
 */

//WPMU
function dpa_register_blogcommentranked() { add_action( 'comment_post', 'dpa_handle_blogcommentranked', 10, 2 ); }
function dpa_register_blogranked() { add_action( 'publish_post', 'dpa_handle_blogranked', 10, 2 ); }
function dpa_register_blogwelcome() { add_action( 'wpmu_new_blog', 'dpa_handle_blogwelcome', 10, 2 ); }

//BP; these have to use counters, mostly because of XMLRPC.
function dpa_register_forumranked() { add_action( 'bp_forums_new_topic', 'dpa_handle_forumranked', 10, 1 ); }
function dpa_register_replyranked() { add_action( 'bp_forums_new_post', 'dpa_handle_replyranked', 10, 1 ); }
function dpa_register_wireranked() { add_action( 'bp_wire_post_posted', 'dpa_handle_wireranked', 10, 3 ); }

//BP
function dpa_register_friendask() { add_action( 'friends_friendship_requested', 'dpa_handle_friendask', 10, 3 ); }
function dpa_register_groupcreated() { add_action( 'groups_created_group', 'dpa_handle_groupcreated', 10, 1 ); }
function dpa_register_groupinvitation() { add_action( 'groups_invite_user', 'dpa_handle_groupinvitation', 10, 2 ); }
function dpa_register_groupjoined() { add_action( 'groups_join_group', 'dpa_handle_groupjoined', 10, 2 ); }
function dpa_register_grouppromoted() { add_action( 'groups_promoted_member', 'dpa_handle_grouppromoted', 10, 2 ); }
function dpa_register_messageranked() { add_action( 'messages_send_message', 'dpa_handle_messageranked', 10, 1 ); }
function dpa_register_profileupdate() { add_action( 'xprofile_updated_profile', 'dpa_handle_profileupdate', 10, 1 ); }
function dpa_register_profileactivated() { add_action( 'bp_activation_extras', 'dpa_handle_profileactivated', 10, 2 ); }
function dpa_register_profileavatar() { add_action( 'bp_core_avatar_save', 'dpa_handle_profileavatar', 10, 4 ); }

function dpa_handle_blogcommentranked( $comment_ID, $comment_approved ) {
	global $bp, $wpdb;

	$user_id = $bp->loggedin_user->id;
	$one     = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'blogcommentranked_1', 'counter' => true ) );
	$thirty  = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'blogcommentranked_30' ) );
	$hundred = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'blogcommentranked_100' ) );

	if ( $hundred->is_unlocked() )
	  return;

	$one->increment_counter();
	if ( 1 == $one->counter ) {	$one->unlock_achievement(); }
	else if ( 30 == $one->counter ) { $thirty->unlock_achievement(); }
	else if ( 100 == $one->counter ) { $hundred->unlock_achievement(); }
}

function dpa_handle_blogranked( $post_id, $post ) {
	global $bp, $wpdb;

	$user_id = $bp->loggedin_user->id;
	$one     = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'blogranked_1', 'counter' => true ) );
	$thirty  = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'blogranked_30' ) );
	$hundred = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'blogranked_100' ) );

	if ( $hundred->is_unlocked() )
	  return;

	$one->increment_counter();
	if ( 1 == $one->counter ) { $one->unlock_achievement(); }
	else if ( 30 == $one->counter ) { $thirty->unlock_achievement(); }
	else if ( 100 == $one->counter ) { $hundred->unlock_achievement(); }
}

function dpa_handle_forumranked( $topic_id ) {
	global $bp, $wpdb;

	$user_id = $bp->loggedin_user->id;
	$one     = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'forumranked_1', 'counter' => true ) );
	$thirty  = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'forumranked_30' ) );
	$hundred = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'forumranked_100' ) );

	if ( $hundred->is_unlocked() )
	  return;

	$one->increment_counter();
	if ( 1 == $one->counter ) { $one->unlock_achievement(); }
	else if ( 30 == $one->counter ) { $thirty->unlock_achievement(); }
	else if ( 100 == $one->counter ) { $hundred->unlock_achievement(); }	
}

function dpa_handle_replyranked( $post_id ) {
	global $bp, $wpdb;

	$user_id = $bp->loggedin_user->id;
	$one     = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'replyranked_1', 'counter' => true ) );
	$thirty  = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'replyranked_30' ) );
	$hundred = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'replyranked_100' ) );

	if ( $hundred->is_unlocked() )
	  return;

	$one->increment_counter();
	if ( 1 == $one->counter ) { $one->unlock_achievement(); }
	else if ( 30 == $one->counter ) { $thirty->unlock_achievement(); }
	else if ( 100 == $one->counter ) { $hundred->unlock_achievement(); }
}

function dpa_handle_messageranked( $message ) {
	global $bp, $wpdb;

	$user_id = $bp->loggedin_user->id;
	$one     = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'messageranked_1' ) );
	$thirty  = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'messageranked_30' ) );
	$hundred = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'messageranked_100' ) );

	if ( $hundred->is_unlocked() )
	  return;

	$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(sender_id) FROM {$bp->messages->table_name_messages} WHERE sender_id = %d", $user_id ) );
	if ( $count >= 1 && !$one->is_unlocked() ) { $one->unlock_achievement(); }
	if ( $count >= 30 && !$thirty->is_unlocked() ) { $thirty->unlock_achievement(); }
	if ( $count >= 100 && !$hundred->is_unlocked() ) { $hundred->unlock_achievement(); }
}

function dpa_handle_wireranked( $id, $item_id, $user_id ) {
	$one     = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'wireranked_1', 'counter' => true ) );
	$thirty  = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'wireranked_30' ) );
	$hundred = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'wireranked_100' ) );

	if ( $hundred->is_unlocked() )
	  return;

	$one->increment_counter();
	if ( 1 == $one->counter ) { $one->unlock_achievement(); }
	else if ( 30 == $one->counter ) { $thirty->unlock_achievement(); }
	else if ( 100 == $one->counter ) { $hundred->unlock_achievement(); }
}

function dpa_handle_profileavatar( $user_id, $old_img_path, $new_href, $img_path ) {
	$profileavatar = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'profileavatar' ) );
	if ( $profileavatar->is_unlocked() )
	  return;

  $profileavatar->unlock_achievement();
}

function dpa_handle_profileactivated( $user_id, $meta ) {
	$profileactivated = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'profileactivated' ) );
	if ( $profileactivated->is_unlocked() )
	  return;

  $profileactivated->unlock_achievement();
}

function dpa_handle_profileupdate( $group_id ) {
	global $bp;

	$profileupdate = new dpa_achievement( array( 'user_id' => $bp->loggedin_user->id, 'name' => 'profileupdate' ) );
	if ( $profileupdate->is_unlocked() )
	  return;

  $profileupdate->unlock_achievement();
}

function dpa_handle_groupjoined( $group_id, $user_id ) {
	$groupjoined = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'groupjoined' ) );
	if ( $groupjoined->is_unlocked() )
	  return;

  $groupjoined->unlock_achievement();
}

function dpa_handle_groupinvitation( $group_id, $user_id  ) {
	global $bp;

	$groupinvitation = new dpa_achievement( array( 'user_id' => $bp->loggedin_user->id, 'name' => 'groupinvitation' ) );
	if ( $groupinvitation->is_unlocked() )
	  return;

  $groupinvitation->unlock_achievement();
}

function dpa_handle_groupcreated( $group_id ) {
	global $bp;

	$groupcreated = new dpa_achievement( array( 'user_id' => $bp->loggedin_user->id, 'name' => 'groupcreated' ) );
	if ( $groupcreated->is_unlocked() )
	  return;

 	$groupcreated->unlock_achievement();
}

function dpa_handle_grouppromoted( $user_id, $group_id ) {
	$grouppromoted = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'grouppromoted' ) );
	if ( $grouppromoted->is_unlocked() )
	  return;

  $grouppromoted->unlock_achievement();
}

function dpa_handle_friendask( $friendship_id, $initiator_user_id, $friend_user_id ) {
	$friendask = new dpa_achievement( array( 'user_id' => $initiator_user_id, 'name' => 'friendask' ) );
	if ( $friendask->is_unlocked() )
	  return;

  $friendask->unlock_achievement();
}

function dpa_handle_blogwelcome( $blog_id, $user_id ) {
	$blogwelcome = new dpa_achievement( array( 'user_id' => $user_id, 'name' => 'blogwelcome' ) );
	if ( $blogwelcome->is_unlocked() )
	  return;

  $blogwelcome->unlock_achievement();
}

function dpa_get_defaultcategories() {
	return array( __( "Blog", 'dp-achievements' ), __( "Friends", 'dp-achievements' ), __( "Groups", 'dp-achievements' ), __( "Messaging", 'dp-achievements' ), __( "Profile", 'dp-achievements' ) );
}

function dpa_get_defaultachievements() {
	$defaults = array(
		array( 'category' => __( "Blog", 'dp-achievements' ),
					 'short_name' => 'blogwelcome',
					 'name' => __( "Welcome to the blogosphere", 'dp-achievements' ),
					 'description' => __( "Created a blog; get posting!", 'dp-achievements' ),
					 'points' => 15 ),
		array( 'category' => __( "Blog", 'dp-achievements' ),
					 'short_name' => 'blogranked_1',
					 'name' => __( "First post", 'dp-achievements' ),
					 'description' => __( "Published your first blog entry.", 'dp-achievements' ),
					 'points' => 15 ),
		array( 'category' => __( "Blog", 'dp-achievements' ),
					 'short_name' => 'blogranked_30',
					 'name' => __( "Flexible Demeanour", 'dp-achievements' ),
					 'description' => "Published your thirtieth blog entry.",
					 'points' => 45 ),
		array( 'category' => __( "Blog", 'dp-achievements' ),
					 'short_name' => 'blogranked_100',
					 'name' => __( "The Ends Of Invention", 'dp-achievements' ),
					 'description' => "One hundred blog posts!",
					 'points' => 75 ),
		array( 'category' => __( "Blog", 'dp-achievements' ),
					 'short_name' => 'blogcommentranked_1',
					 'name' => __( "It's My Party And I'll Sing If I Want To", 'dp-achievements' ),
					 'description' => "Commented on a blog post.",
					 'points' => 10 ),
		array( 'category' => __( "Blog", 'dp-achievements' ),
					 'short_name' => 'blogcommentranked_30',
					 'name' => __( "But Who's Counting?", 'dp-achievements' ),
					 'description' => __( "You’ve left 30 comments on blogs.", 'dp-achievements' ),
					 'points' => 30 ),
		array( 'category' => __( "Blog", 'dp-achievements' ),
					 'short_name' => 'blogcommentranked_100',
					 'name' => __( "You'll Thank Me Later", 'dp-achievements' ),
					 'description' => __( "You’ve left 100 comments on blogs.", 'dp-achievements' ),
					 'points' => 50 ),
 		array( 'category' => __( "Friends", 'dp-achievements' ),
					 'short_name' => 'friendask',
					 'name' => __( "I Choo-Choo-Choose You!", 'dp-achievements' ),
					 'description' => __( "You’ve sent a friendship request to a member of the site.", 'dp-achievements' ),
					 'points' => 10 ),
		array( 'category' => __( "Groups", 'dp-achievements' ),
					 'short_name' => 'grouppromoted',
 					 'name' => __( "You’ve got the Bridge, Number One", 'dp-achievements' ),
					 'description' => __( "Promoted to a moderator of a group.", 'dp-achievements' ),
					 'points' => 30 ),
		array( 'category' => __( "Groups", 'dp-achievements' ),
					 'short_name' => 'groupcreated',
					 'name' => __( "Open social", 'dp-achievements' ),
					 'description' => __( "Created a group.", 'dp-achievements' ),
					 'points' => 10 ),
		array( 'category' => __( "Groups", 'dp-achievements' ),
					 'short_name' => 'groupinvitation',
					 'name' => __( "All are welcome", 'dp-achievements' ),
					 'description' => __( "Sent an invitation for someone to join a group which you moderate.", 'dp-achievements' ),
					 'points' => 10 ),
		array( 'category' => __( "Groups", 'dp-achievements' ),
					 'short_name' => 'groupjoined',
					 'name' => __( "Bonjour!", 'dp-achievements' ),
					 'description' => __( "Joined a group.", 'dp-achievements' ),
					 'points' => 10 ),
		array( 'category' => __( "Messaging", 'dp-achievements' ),
					 'short_name' => 'wireranked_1',
					 'name' => __( "First time for everything", 'dp-achievements' ),
					 'description' => __( "Posted on a wire.", 'dp-achievements' ),
					 'points' => 10 ),
		array( 'category' => __( "Messaging", 'dp-achievements' ),
					 'short_name' => 'wireranked_30',
					 'name' => __( "Second time for everything", 'dp-achievements' ),
					 'description' => __( "You’ve left 30 wire posts.", 'dp-achievements' ),
					 'points' => 30 ),
		array( 'category' => __( "Messaging", 'dp-achievements' ),
					 'short_name' => 'wireranked_100',
					 'name' => __( "Third time for everything", 'dp-achievements' ),
					 'description' => __( "You’ve left 100 wire posts.", 'dp-achievements' ),
					 'points' => 50 ),
		array( 'category' => __( "Messaging", 'dp-achievements' ),
					 'short_name' => 'messageranked_1',
					 'name' => __( "First Class post", 'dp-achievements' ),
					 'description' => __( "Sent a message to someone.", 'dp-achievements' ),
					 'points' => 10 ),
		array( 'category' => __( "Messaging", 'dp-achievements' ),
					 'short_name' => 'messageranked_30',
					 'name' => __( "Second Class post", 'dp-achievements' ),
					 'description' => __( "Sent 30 messages.", 'dp-achievements' ),
					 'points' => 30 ),
		array( 'category' => __( "Messaging", 'dp-achievements' ),
					 'short_name' => 'messageranked_100',
					 'name' => __( "Special Delivery", 'dp-achievements' ),
					 'description' => __( "Sent 100 messages.", 'dp-achievements' ),
					 'points' => 50 ),
		array( 'category' => __( "Messaging", 'dp-achievements' ),
					 'short_name' => 'forumranked_1',
					 'name' => __( "Never Talk To Strangers", 'dp-achievements' ),
					 'description' => __( "Started a forum topic.", 'dp-achievements' ),
					 'points' => 10 ),
		array( 'category' => __( "Messaging", 'dp-achievements' ),
					 'short_name' => 'forumranked_30',
					 'name' => __( "Allow me to elaborate", 'dp-achievements' ),
					 'description' => __( "Started 30 forum topics.", 'dp-achievements' ),
					 'points' => 30 ),
		array( 'category' => __( "Messaging", 'dp-achievements' ),
					 'short_name' => 'forumranked_100',
					 'name' => __( "Happy Idiot Talk", 'dp-achievements' ),
					 'description' => __( "Started 100 forum topics.", 'dp-achievements' ),
					 'points' => 50 ),
		array( 'category' => __( "Messaging", 'dp-achievements' ),
					 'short_name' => 'replyranked_1',
					 'name' => __( "Frank Exchange Of Views", 'dp-achievements' ),
					 'description' => __( "Left a reply on a forum topic.", 'dp-achievements' ),
					 'points' => 10 ),
		array( 'category' => __( "Messaging", 'dp-achievements' ),
					 'short_name' => 'replyranked_30',
					 'name' => __( "Quietly Confident", 'dp-achievements' ),
					 'description' => __( "Replied to 30 forum topics.", 'dp-achievements' ),
					 'points' => 30 ),
		array( 'category' => __( "Messaging", 'dp-achievements' ),
					 'short_name' => 'replyranked_100',
					 'name' => __( "Revisionist", 'dp-achievements' ),
					 'description' => __( "Replied to 100 forum topics.", 'dp-achievements' ),
					 'points' => 50 ),
		array( 'category' => __( "Profile", 'dp-achievements' ),
					 'short_name' => 'profileupdate',
					 'name' => __( "Identity crisis", 'dp-achievements' ),
					 'description' => __( "Updated the information on your profile.", 'dp-achievements' ),
					 'points' => 15 ),
		array( 'category' => __( "Profile", 'dp-achievements' ),
					 'short_name' => 'profileactivated',
					 'name' => __( "Welcome", 'dp-achievements' ),
					 'description' => __( "You’ve activated your account on this website - welcome!", 'dp-achievements' ),
					 'points' => 10 ),
		array( 'category' => __( "Profile", 'dp-achievements' ),
					 'short_name' => 'profileavatar',
					 'name' => __( "She’s got that Mona Lisa smile", 'dp-achievements' ),
					 'description' => __( "Uploaded or updated the avatar picture to your account.", 'dp-achievements' ),
					 'points' => 15 )
	);

	return $defaults;
}
?>